<?php
/**
 * created by zzy
 * date: 2018/1/26 14:07
 */


namespace App\Api\Service\Waste\Controllers;


use App\Eloquent\Area;
use App\Eloquent\City;
use App\Eloquent\Province;
use App\Eloquent\Ygt\DepartmentUser;
use App\Eloquent\Ygt\OrdertypeProcessWaste;
use App\Eloquent\Ygt\Product;
use App\Eloquent\Ygt\ProductFields;
use App\Eloquent\Ygt\User;
use App\Eloquent\Ygt\Waste;
use App\Eloquent\Ygt\WasteBuyer;
use App\Engine\Func;
use Framework\BaseClass\Api\Controller;

class WasteController extends Controller
{
    public $userId        = 25;
    public function lists()
    {
        $userId             = Func::getHeaderValueByName('userid',$this->userId);
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        $limit              = request('limit',10);
        $page               = request('page',1);
        $keyword            = request('keyword');


        $offset             = ($page-1)*$limit;
        if(!$userInfo)
        {
            xThrow( ERR_PARAMETER );
        }
        $companyId          = $userInfo->company_id;
        $where              = [ 'company_id' => $companyId ];
        if($keyword){
            $where              = [ 'waste_name'=>['like',"%$keyword%"] ];
        }

        $count              = Waste::getCount($where);
        $wasteList          = Waste::getList($where,'',$limit,$offset)->toArray();
        $productIdArr       = $processWasteIdArr = [];
        foreach($wasteList as $key=>$val)
        {
            $valRelateType      = $val['relate_type'];//相关类型 1-材料 2-工序废品
            $valRelateId        = $val['relate_id'];
            if($valRelateType == 1){
                $productIdArr[]         = $valRelateId;
//                //获取材料集合ID列表
//                $productIdArr = \App\Eloquent\Ygt\Product::whereIn('id',$productIdArr)->get()->pluck('assemblage_material_id');
            }else{
                $processWasteIdArr[]    = $valRelateId;
            }
        }
        $wasteList          = $this->changeWasteList($wasteList);

        if(!empty($productIdArr))
        {
            $wasteList      = $this->getWasteProductList($wasteList,$productIdArr);
        }
        if(!empty($processWasteIdArr))
        {
            $wasteList      = $this->getWasteProcessList($wasteList,$processWasteIdArr);
        }

        $listData               = [
            'waste_list'=>$wasteList
        ];
        $pagingData = [
            'page' => $page,
            'pageSize' => $limit,
            'total' => $count,
            'keyword' => ''
        ];
        return $this->pagingData($wasteList, $pagingData);
    }
    //取废料列表中-相关联材料的信息
    private function getWasteProductList($wasteList,$idArr)
    {
        //取列表
        $where              = [
            'ygt_product.id'=>['in',$idArr]
        ];
        $limit              = '';
        $productList        = Product::getListByJoin($where,$limit);
        $assemblageMaterialIdArr = $productList->pluck('assemblage_material_id');
        $productList = $productList->toArray();

        //取材料的自定义属性值
        $where              = ['assemblage_material_id'=>['in',$assemblageMaterialIdArr]];
        $customFieldsArr    = ProductFields::getTransformListToArr($where);
        //转换列表
        $otherArr           = ['custom_fields_arr'=>$customFieldsArr];
        $lists              = Product::transformListByType($productList,$otherArr);
        $result             = $this->changeWasteList($wasteList,$lists,1);
        return $result;
    }
    //取废料列表中-相关联工序废品的信息
    private function getWasteProcessList($wasteList,$idArr)
    {
        $lists              = OrdertypeProcessWaste::withTrashed()->whereIn('id',$idArr)->get()->toArray();
        $result             = $this->changeWasteList($wasteList,$lists,2);
        return $result;
    }
    //取废料列表中-返回值字段统一问题
    private function changeWasteList($wasteList,$relateList=[],$relateType=0)
    {
        $result             = [];
        switch($relateType)
        {
            case 1:
                //材料
                foreach($wasteList as $key=>$val)
                {
                    $arr                    = $val;
                    $valId                  = $val['id'];
                    $valRelateType          = $val['relate_type'];
                    $valRelateId            = $val['relate_id'];
                    $valNumber              = $val['number'];
                    $valTotalNumber         = $val['total_number'];
                    $valTotalSellerNumber   = $val['total_seller_number'];
                    $wasteName   = $val['waste_name'];
                    foreach($relateList as $k=>$v)
                    {
                        $vId                = $v['id'];
                        if($valRelateType==$relateType && $valRelateId == $vId)
                        {
                            if($valTotalNumber){
                                $ratio = round($valNumber/$valTotalNumber*100);
                            }else{
                                $ratio = 0;
                            }

                            $arr            = [
                                'id'=>$valId,
                                'relate_type'=>$relateType,
                                'relate_id'=>$valRelateId,
                                'number'=>$valNumber,
                                'total_number'=>$valTotalNumber,
                                'total_seller_number'=>$valTotalSellerNumber,
                                'product_name'=>$wasteName,
                                'waste_name'=>$wasteName,

                                'product_no'=>$v['product_no'],
                                'is_public'=>$v['is_public'],
                                'barcode'=>$v['barcode'],
                                'warning_number'=>$v['warning_number'],
                                'full_number'=>$v['full_number'],
                                'category_id'=>$v['category_id'],
                                'price'=>$v['price'],
                                'unit'=>$v['unit'],
                                'customer_id'=>$v['customer_id'],
                                'created_at'=>$val['created_at'],
                                'img_url'=>$v['img_url'],
                                'seller_company_name'=>$v['seller_company_name'],
                                'customer_name'=>$v['customer_name'],
                                'image_path'=>$v['image_path'],
                                'is_warning'=>$v['is_warning'],
                                'circle_front_color'=>$v['circle_front_color'],
                                'circle_back_color'=>$v['circle_back_color'],
                                'ratio'=>$ratio,
                                'is_new'=>$v['is_new'],
                                'custom_fields'=>$v['custom_fields'],
                                'first_text'=>$v['first_text'],
                                'second_text'=>$v['second_text'],
                                'third_text'=>$v['third_text'],
                                'custom_fields_text'=>$v['custom_fields_text'],
                            ];
                        }
                    }
                    $result[]       = $arr;
                }
                break;
            case 2:
                //工序废品
                foreach($wasteList as $key=>$val)
                {

                    $arr                    = $val;
                    $valId                  = $val['id'];
                    $valRelateType          = $val['relate_type'];
                    $valRelateId            = $val['relate_id'];
                    $valNumber              = $val['number'];
                    $valTotalNumber         = $val['total_number'];
                    $valTotalSellerNumber   = $val['total_seller_number'];

                    $wasteName   = $val['waste_name'];
                    foreach($relateList as $k=>$v)
                    {
                        $vId                = $v['id'];
                        $vTitle             = $v['title'];
                        if($valRelateType==$relateType && $valRelateId == $vId)
                        {
                            if($valTotalNumber){
                                $ratio = round($valNumber/$valTotalNumber*100);
                            }else{
                                $ratio = 0;
                            }

                            $arr            = [
                                'id'=>$valId,
                                'relate_type'=>$relateType,
                                'relate_id'=>$valRelateId,
                                'number'=>$valNumber,
                                'total_number'=>$valTotalNumber,
                                'total_seller_number'=>$valTotalSellerNumber,
                                'product_name'=> $wasteName,
                                'waste_name'=> $wasteName,

                                'product_no'=>'',
                                'is_public'=>'yes',
                                'barcode'=>'',
                                'warning_number'=>0,
                                'full_number'=>0,
                                'category_id'=>0,
                                'price'=>0.00,
                                'unit'=>'千克',
                                'customer_id'=>0,
                                'created_at'=>$val['created_at'],
                                'img_url'=>'',
                                'seller_company_name'=>'',
                                'customer_name'=>'',
                                'image_path'=>'',
                                'is_warning'=>0,
                                'circle_front_color'=>'#FF5252',
                                'circle_back_color'=>'#FFEDED',
                                'ratio'=> $ratio,
                                'is_new'=>0,
                                'custom_fields'=>[],
                                'first_text'=>'',
                                'second_text'=>'',
                                'third_text'=>'',
                                'custom_fields_text'=>''
                            ];
                        }
                    }
                    $result[]       = $arr;
                }
                break;
            default:
                //默认是添加字段默认值
                foreach($wasteList as $key=>$val)
                {
                    $arr                = [
                        'id'=>$val['id'],
                        'relate_type'=>$val['relate_type'],
                        'relate_id'=>$val['relate_id'],
                        'number'=>$val['number'],
                        'total_number'=>$val['total_number'],
                        'total_seller_number'=>$val['total_seller_number'],
                        'waste_name'=>$val['waste_name'],

                        'product_name'=>'',
                        'product_no'=>'',
                        'is_public'=>'yes',
                        'barcode'=>'',
                        'warning_number'=>0,
                        'full_number'=>0,
                        'category_id'=>0,
                        'price'=>0.00,
                        'unit'=>'千克',
                        'customer_id'=>0,
                        'created_at'=>$val['created_at'],
                        'img_url'=>'',
                        'seller_company_name'=>'',
                        'customer_name'=>'',
                        'image_path'=>'',
                        'is_warning'=>0,
                        'circle_front_color'=>'#FF5252',
                        'circle_back_color'=>'#FFEDED',
                        'ratio'=>0,
                        'is_new'=>0,
                        'custom_fields'=>[],
                        'first_text'=>'',
                        'second_text'=>'',
                        'third_text'=>'',
                        'custom_fields_text'=>''
                    ];
                    $result[]       = $arr;
                }
        }
        return $result;
    }
}